﻿import com.crystalcentral.*;
import com.crystalcentral.model.*;
import com.crystalcentral.view.*;
import com.crystalcentral.util.*;

import mx.utils.Delegate;

class com.crystalcentral.view.MCanvas {
	var diyTool:DiyTool;
	var canvas:MovieClip;
	var mDiyBG:MovieClip;
	var prodImg:String;
	var mItems:Array;
	var currentItem:MItem;
	
	// Constants:
	public static var CLASS_REF = com.crystalcentral.view.MCanvas;
	
	public function MCanvas() {
		// constructor code
		this.diyTool = DiyTool.getInstance();
		
		canvas = _root.createEmptyMovieClip("mCanvas", _root.getNextHighestDepth());
		canvas._x = _root.canvasBackground._x;
		canvas._y = _root.canvasBackground._y;
		canvas.setMask(_root.canvasBackground.duplicateMovieClip("mCanvasMask", _root.getNextHighestDepth()));
		//canvas.menu = _root.diyTool.getContextMenu();
		mDiyBG = canvas.createEmptyMovieClip("mDiyBG", canvas.getNextHighestDepth());
		
		this.mItems = new Array();
		
		validateDepth();
	}
	
	public function loadProductBG(callback:Function) {
		if (_root.productImageSrc == undefined)
		{
			_root.productImageSrc = "images/diy/CC17075_DIY.jpg";
			_root.imageWidth = 648;
			_root.imageHeight = 720;
		}
		prodImg = _root.productImageSrc;
		
		var mcLoader = new MovieClipLoader();
		mcLoader.onLoadInit = Delegate.create(diyTool, callback);
		mcLoader.loadClip(DiyTool.HOST + prodImg, mDiyBG);
	}

	public function validateDepth() {
		_root.global_toolbar.swapDepths(canvas.getDepth() + 1);
		_root.nextLayer.swapDepths(canvas.getDepth() + 2);
		_root.mcHScroll.swapDepths(canvas.getDepth() + 3);
		_root.mcVScroll.swapDepths(canvas.getDepth() + 4);
		_root.mcZoom.swapDepths(canvas.getDepth() + 5);
	}
	
	public function addTextItem(diyItem:DiyTextItem) {
		if (StringTool.trim(diyItem.text) != "") {
			var mText:MText = new MText(diyItem, this);
			this.mItems.push(mText);
		}
	}
	
	public function addImageItem(diyItem:DiyImageItem) {
		var mImage:MImage = new MImage(diyItem, this);
		this.mItems.push(mImage);
	}
	
	public function setAllLostFocus(exceptItem:MItem) {
		for (var i=0; i<mItems.length; i++) {
			if (mItems[i] != exceptItem)
				mItems[i].setLostFocus();
		}
	}
	public function setObjectFocused(mItem:MItem) {
		mItem.setOnFocus();
	}
}
